package com.qingfeng.qfcms.controller.Admin.user;

import com.octo.captcha.service.image.ImageCaptchaService;
import com.qingfeng.qfcms.common.web.session.SessionProvider;
import com.qingfeng.qfcms.contant.Constants;
import com.qingfeng.qfcms.controller.BaseController;
import com.qingfeng.qfcms.domin.User;
import com.qingfeng.qfcms.service.UserService;
import com.qingfeng.qfcms.utils.DateUtil;
import com.qingfeng.qfcms.utils.VerifyCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Date;

/**
 * ============================================================
 * 版权 ：青峰软件有限公司  版权所有 (c) 2016
 * 作者:  menxd
 * 版本 ：1.0
 * 创建日期 ： 2016/7/21
 * 描述 ：
 * <p>
 * 修订历史 ：
 * ============================================================
 */
@Controller
@RequestMapping("base")
public class LoginController extends BaseController {



    @Autowired
    private SessionProvider sessionProvider;
    @Autowired
    private ImageCaptchaService imageCaptchaService;
    @Resource
    private UserService userService;
    @RequestMapping(value = {"toLogin.shtml",""})
    public String toLogin(){

        return "admin/login";
    }

    @RequestMapping("login")
    public String login(String username, String password, HttpServletRequest request){
        User b= userService.checkName(username,password);

        if(b!=null){
            sessionProvider.setAttribute(request, Constants.USER_SESSION,b);
            b.setIP(request.getRemoteHost());
            b.setLAST_LOGIN(DateUtil.dateToString(new Date(),DateUtil.FORMAT_LONG));
            userService.updateIp(b);
            return "redirect:/admin/index.shtml";
        }else {
            request.getSession().setAttribute("msg","用户名或密码错误");
            return "redirect:/base/toLogin.shtml";
        }


    }

    @RequestMapping(value = "VerifyCode.shtml",method = RequestMethod.GET)
    public void VerifyCode(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
        VerifyCode vc=new VerifyCode();
        BufferedImage image = vc.getImage();//获取一次性验证码图片
        // 该方法必须在getImage()方法之后来调用
        System.out.println(vc.getText());//获取图片上的文本
        VerifyCode.output(image, response.getOutputStream());//把图片写到指定流中

        System.out.println();

        // 把文本保存到session中，为LoginServlet验证做准备
        session.setAttribute("vCode", vc.getText());
        //request.getSession().setAttribute("vCode", vc.getText());
    }

    @RequestMapping("checkCode.shtml")
    @ResponseBody
    public boolean checkCode(String code,HttpServletRequest request){
        System.out.println(code);
        System.out.println(sessionProvider.getSessionId(request));
        if (imageCaptchaService.validateResponseForID(sessionProvider.getSessionId(request), code)){
             return true;
        }
        return false;

    }

    @RequestMapping("/loginOut.shtml")
    public String loginOut(HttpServletRequest request){
        sessionProvider.logout(request);
        return "redirect:/base/toLogin.shtml";
    }
}
